#include "list.h"

node *list_init()
{
     node *ptr = malloc(sizeof(node));
     if (ptr == NULL)
          return NULL;
     ptr->next = NULL;
     return ptr;
}
void list_create(node *ptr, int *a, int n)
{
     node *p = ptr;
     for (int i = 0; i < n; i++)
     {
          node *new = malloc(sizeof(node));
          if (new == NULL)
               exit(1);
          new->next = NULL;
          new->data = a[i];
          p->next = new;
          p = new;
     }
}
void list_show(node *ptr)
{
     node *p = ptr;
     while (p->next != NULL)
     {
          p = p->next;
          printf("%d  ", p->data);
     }
     printf("\n");
}
node *list_locate(node *ptr, int i)
{
     node *p = ptr;
     int num = 0;
     while(num++ != i)
          p = p->next;
     return p;
}
node *list_max(node *ptr)
{
     node *p = ptr->next;
     node *addr = p;

     int max = p->data;

     while (p != NULL)
     {
          if (p->data > max)
          {
               max = p->data;
               addr = p;
          }
          p = p->next;
     }
     return addr;
}
int list_number(node *ptr, int x)
{
     node *p = ptr->next;
     int count = 0;
     while (p != NULL)
     {
          if(p->data == x)
               count++;
          p = p->next;
     }
     return count;
}
void tidyup(node *ptr)
{
     node *p = ptr->next;
     node *q = p->next;
     while (q != NULL)
     {
          if (p->data == q->data)
          {
               p->next = q->next;
               free(q);
               q = p->next;
          }
          else
          {
               p = p->next;
               q = q->next;
          }
     }
}